using System;

using Geekees.App.Cashbook.Lib.BLL;
using Geekees.App.Cashbook.Lib.Entity;
using Geekees.App.Cashbook.Lib.Exceptions;
using Geekees.App.Cashbook.Lib.Helper;

namespace Geekees.App.Cashbook.Lib.Helper
{
	/// <summary>
	/// Summary description for LoginManager.
	/// </summary>
	public class LoginManager
	{
		public LoginManager()
		{
			
		}

		public static User GetCurrentUser()
		{
			User u = SessionVar.Get( Consts.SESSION_KEY_CURRENT_USER ) as User;
			if( u == null )
				throw new LoginException();

			return u;
		}

		public static bool Login( string userName, string pwd )
		{
			User u = BLSecurity.LoginUser( userName, pwd );
			if( u == null )
				return false;

			SessionVar.Set( Consts.SESSION_KEY_CURRENT_USER, u );
			BLSecurity.UpdateLastLoginTime( userName, DateTime.Now );
			return true;
		}

		public static bool LoginUsingTicket( string userName, string ticket )
		{
			User u = BLSecurity.LoginUserUsingTicket( userName, ticket );
			if( u == null )
				return false;

			SessionVar.Set( Consts.SESSION_KEY_CURRENT_USER, u );
			BLSecurity.UpdateLastLoginTime( userName, DateTime.Now );
			return true;
		}

		public static void Logout()
		{
			SessionVar.Set( Consts.SESSION_KEY_CURRENT_USER, null );
		}
	}
}
